/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sheep.view.Component;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.RoundRectangle2D;
import java.awt.geom.RoundRectangle2D.Float;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.ListIterator;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import sheep.controller.Workspace;
import sheep.model.Note;


public class DayGridPanel extends JPanel{

 private int line, column;
 private int columnLeftWidth, columnRightWidth;
 private float beginTime= 7.f;
 private float endTime= 19.f;
  
 private float beginLunchTime= 12.f;
 private float endLunchTime = 13.5f;
  
 private int[] ArrowX,beginArrowY,endArrowY,beginLunchArrowY,endLunchArrowY;
 private int[] resizeArrowX, resizeArrowY;
 
 private GradientPaint   p1 = new GradientPaint(0, 0, new Color(0, 0, 0), 0, getHeight() - 1, new Color(100, 100, 100));
 private GradientPaint   p2 = new GradientPaint(0, 1, new Color(0, 0, 0, 50), 0, getHeight() - 3, new Color(255, 255, 255, 100));
 
 private boolean dragBegin = false;
 private boolean dragEnd = false;
 private boolean dragBeginLunch = false;
 private boolean dragEndLunch = false;
 
 private RoundRectangle2D.Float r2d1, r2d2, r2d3;
 private Shape clip1;
 private Graphics2D g2d; 
 private int pas = 40; 
 private JPanel notePanel; 

 private JMenuItem ds = new JMenuItem("Nouvelle note");
 JMenuItem es = new JMenuItem("Editer note");
 JMenuItem ss = new JMenuItem("supprimer note");
 
 private JPopupMenu popup = new JPopupMenu(); 
 private Workspace workSpace;
 public LinkedList<Note> noteList = new LinkedList<Note>(); 
 public LinkedList<Note> sortedList;
 private GradientPaint gradient;
  private Float r2d4;
  
  public DayGridPanel(int line, int column,  Workspace newWorkSpace ){
    
   this.workSpace = newWorkSpace;
   noteList =  workSpace.getNoteList();
   sortedList = new LinkedList<Note>();
   //currentDay.add(Calendar.DAY_OF_YEAR, 1);
   this.line = line;
   this.column = column;
    
   setOpaque(false);
    
   columnLeftWidth = 100;
   
   ArrowX= new int[3];
   beginArrowY= new int[3];
   endArrowY= new int[3];
   beginLunchArrowY= new int[3];
   endLunchArrowY= new int[3];    
    
    resizeArrowX = new int [3]; 
    resizeArrowY = new int [3]; 
    
   ArrowX[0]= columnLeftWidth;    
   ArrowX[1]= columnLeftWidth-10; 
   ArrowX[2]= columnLeftWidth-10; 
   
   beginArrowY[0]= pas/2+ (int)(beginTime* pas);
   beginArrowY[1]= pas/2+10 +(int)(beginTime* pas);
   beginArrowY[2]= pas/2-10+ (int)(beginTime* pas);
   
   endArrowY[0]= pas/2 +(int)(endTime)*pas;
   endArrowY[1]= pas/2+10 +(int)(endTime)*pas;
   endArrowY[2]= pas/2 -10 +(int)(endTime)*pas;
      
   beginLunchArrowY[0]= pas/2+ (int)(beginLunchTime* pas);
   beginLunchArrowY[1]= pas/2+10+ (int)(beginLunchTime* pas);
   beginLunchArrowY[2]= pas/2-10+ (int)(beginLunchTime* pas);
   
    endLunchArrowY[0]= pas/2 +(int)(endLunchTime)*pas;
    endLunchArrowY[1]= pas/2+10 +(int)(endLunchTime)*pas;
    endLunchArrowY[2]= pas/2-10 +(int)(endLunchTime)*pas;
    
    popup.add(ds);
    popup.add(es);
    popup.add(ss);
    
    es.addMouseListener(new MouseListener(){

      public void mouseClicked(MouseEvent e) {
       popup.setVisible(false);
        notePanel.setVisible(true);      
      }

      public void mousePressed(MouseEvent e) {}
      public void mouseReleased(MouseEvent e) {}
      public void mouseEntered(MouseEvent e) { }
      public void mouseExited(MouseEvent e) { }     
      });
     
      
      
    ss.addMouseListener(new MouseListener(){
        
      public void mouseClicked(MouseEvent e) {
        popup.setVisible(false); 
        System.out.println("suppression ");
 
        System.out.println("suppression " + workSpace.getSelectedNote());
        workSpace.getSelectedNote().getCalendrier().getNoteList().remove(workSpace.getSelectedNote());
        noteList.remove(workSpace.getSelectedNote());
        sortedList.remove(workSpace.getSelectedNote());
        workSpace.getNoteList().remove(workSpace.getSelectedNote());
        workSpace.setSelectedNote(null);
        
        repaint();       
      }

      public void mousePressed(MouseEvent e) {}
      public void mouseReleased(MouseEvent e) {}
      public void mouseEntered(MouseEvent e) { }
      public void mouseExited(MouseEvent e) { }     
      });    
    
    
    
    ds.addMouseListener(new MouseListener(){

      public void mouseClicked(MouseEvent e) {
        
        Note note3 = new Note();
        note3.setCalendrier(workSpace.getCurrentCalendrier());
        workSpace.getCurrentCalendrier().addNote(note3);
        int yPos = (int) (popup.getLocationOnScreen().getY()- DayGridPanel.this.getLocationOnScreen().getY()-pas/2)/ pas ;
        
        
        Calendar temp = Calendar.getInstance(workSpace.getCurrentDay().getTimeZone());
        //temp.add(Calendar.DAY_OF_YEAR, -1);
        temp.add(Calendar.DAY_OF_YEAR, workSpace.getCurrentDay().get(Calendar.DAY_OF_YEAR)-temp.get(Calendar.DAY_OF_YEAR));      
        //temp.add(Calendar.DAY_OF_YEAR, - ((temp.get(Calendar.DAY_OF_WEEK)+5)%7)+xPos-1);
        temp.set(Calendar.HOUR_OF_DAY, (int)(yPos));
        temp.set(Calendar.MINUTE, (((popup.getLocationOnScreen().getY()- DayGridPanel.this.getLocationOnScreen().getY()-pas/2)% pas)<10? 0 :
                                    ((popup.getLocationOnScreen().getY()- DayGridPanel.this.getLocationOnScreen().getY()-pas/2)% pas)<20? 15: 
                                    ((popup.getLocationOnScreen().getY()- DayGridPanel.this.getLocationOnScreen().getY()-pas/2)% pas)<30? 30: 45 ));   
        
        Calendar temp2 = Calendar.getInstance(workSpace.getCurrentDay().getTimeZone());  
        //temp2.add(Calendar.DAY_OF_YEAR, -1);
        temp2.set(Calendar.HOUR_OF_DAY, (int)(yPos));  
        temp2.add(Calendar.DAY_OF_YEAR, workSpace.getCurrentDay().get(Calendar.DAY_OF_YEAR)-temp.get(Calendar.DAY_OF_YEAR));              
        temp2.add(Calendar.HOUR_OF_DAY, 1);
        
        temp2.set(Calendar.MINUTE, (((popup.getLocationOnScreen().getY()- DayGridPanel.this.getLocationOnScreen().getY()-pas/2)% pas)<10? 0 :
                                    ((popup.getLocationOnScreen().getY()- DayGridPanel.this.getLocationOnScreen().getY()-pas/2)% pas)<20? 15: 
                                    ((popup.getLocationOnScreen().getY()- DayGridPanel.this.getLocationOnScreen().getY()-pas/2)% pas)<30? 30: 45 ));   
        note3.setDate(temp);
        note3.setEndDate(temp2);
        
        note3.setTitle("Nouvelle note");
        note3.setSelected(true);
        note3.setDragBottomEnable(true);
        workSpace.setSelectedNote(note3);
        noteList.add(note3);
        popup.setVisible(false);
        repaint();
      }

      public void mousePressed(MouseEvent e) {}
      public void mouseReleased(MouseEvent e) {}
      public void mouseEntered(MouseEvent e) {}
      public void mouseExited(MouseEvent e) {}
     
     });  
          
    this.addMouseMotionListener(new MouseMotionListener(){
      
      public void mouseMoved(MouseEvent e) {
        int Ymove =e.getY()-e.getY()%(pas/4);
  
        if ( dragBegin ){               
          beginArrowY[0]= Ymove;              
          beginArrowY[1]= Ymove+10;              
          beginArrowY[2]= Ymove-10;
        }else if (dragEnd){                
          endArrowY[0]= Ymove;               
          endArrowY[1]= Ymove+10;              
          endArrowY[2]= Ymove-10;  
        }else if ( dragBeginLunch ){               
          beginLunchArrowY[0]= Ymove;               
          beginLunchArrowY[1]= Ymove+10;                
          beginLunchArrowY[2]= Ymove-10;
        }else if (dragEndLunch){               
          endLunchArrowY[0]= Ymove;               
          endLunchArrowY[1]= Ymove+10;               
          endLunchArrowY[2]= Ymove-10;                
        }else {
        if (workSpace.getSelectedNote()!=null)
          
            if (workSpace.getSelectedNote().isDragTopEnable()){
              if (e.getY() <= workSpace.getSelectedNote().getNoteZone().getY()+ workSpace.getSelectedNote().getNoteZone().height -10. ){
                workSpace.getSelectedNote().getNoteZone().height= workSpace.getSelectedNote().getNoteZone().height - 
                        Ymove + workSpace.getSelectedNote().getNoteZone().y;
                workSpace.getSelectedNote().getNoteZone().y= Ymove;
                workSpace.getSelectedNote().getDate().set(Calendar.HOUR_OF_DAY, (e.getY()-pas/2)/ pas);
                workSpace.getSelectedNote().getDate().set(Calendar.MINUTE, (((e.getY()-pas/2)% pas)<10? 0 :
                                                                              ((e.getY()-pas/2)% pas)<20? 15: 
                                                                              ((e.getY()-pas/2)% pas)<30? 30: 45 ));
              }
            }else if (workSpace.getSelectedNote().isDragBottomEnable() ){
              if (e.getY() >= workSpace.getSelectedNote().getNoteZone().getY() + pas/2){
                workSpace.getSelectedNote().getNoteZone().height= Ymove - workSpace.getSelectedNote().getNoteZone().y;
                workSpace.getSelectedNote().getEndDate().set(Calendar.HOUR_OF_DAY, (int)(e.getY()-pas/2)/ pas);
                workSpace.getSelectedNote().getEndDate().set(Calendar.MINUTE, (((e.getY()-pas/2)% pas)<10? 0 :
                                                                              ((e.getY()-pas/2)% pas)<20? 15: 
                                                                              ((e.getY()-pas/2)% pas)<30? 30: 45 ));
              }
            }else if (workSpace.getSelectedNote().isDragEnable()){
              workSpace.getSelectedNote().getNoteZone().y= Ymove;

              workSpace.getSelectedNote().getDate().set(Calendar.HOUR_OF_DAY, (e.getY()-pas/2)/ pas);
              workSpace.getSelectedNote().getDate().set(Calendar.MINUTE, (((e.getY()-pas/2)% pas)<10? 0 :
                                                                              ((e.getY()-pas/2)% pas)<20? 15: 
                                                                              ((e.getY()-pas/2)% pas)<30? 30: 45 ));
              
              workSpace.getSelectedNote().getEndDate().set(Calendar.HOUR_OF_DAY, (int)(e.getY()-pas/2+ 
                      workSpace.getSelectedNote().getNoteZone().height)/ pas);
              workSpace.getSelectedNote().getEndDate().set(Calendar.MINUTE, (int)( 
                        ((e.getY()-20+workSpace.getSelectedNote().getNoteZone().height)% pas)<10? 0 :
                        ((e.getY()-20+workSpace.getSelectedNote().getNoteZone().height)% pas)<20? 15: 
                        ((e.getY()-20+workSpace.getSelectedNote().getNoteZone().height)% pas)<30? 30: 45 ));
            }
        }
        
 /*     // Pour les notes                           
      ListIterator iter = sortedList.listIterator(sortedList.size());               
      while(iter.hasPrevious()){                 
        Note testNote = (Note) iter.previous();
        int i = testNote.getDate().get(Calendar.DAY_OF_YEAR)- workSpace.getCurrentDay().get(Calendar.DAY_OF_YEAR);
        if (i ==0 ){
        if (testNote.getNoteZone()!=null && testNote.getCalendrier().isDisplay())   
          
          if (e.getX()>= DayGridPanel.this.columnLeftWidth + testNote.getConcurrentPos()*25 &&                       
              e.getX()<= DayGridPanel.this.columnLeftWidth + DayGridPanel.this.columnRightWidth &&                    
              e.getY()>= testNote.getNoteZone().y -5 &&e.getY()<= testNote.getNoteZone().y+ 5 ){     

              break;
            } else if (e.getX()>= DayGridPanel.this.columnLeftWidth + testNote.getConcurrentPos()*25 && 
                      e.getX()<= DayGridPanel.this.columnLeftWidth + columnLeftWidth &&
              e.getX()<= DayGridPanel.this.columnLeftWidth + DayGridPanel.this.columnRightWidth &&  
               e.getY()>= testNote.getNoteZone().getY() + testNote.getNoteZone().height-5 && 
               e.getY()<= testNote.getNoteZone().getY() + testNote.getNoteZone().height +5){
               setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
               break;
            } else if (e.getX()>= DayGridPanel.this.columnLeftWidth + testNote.getConcurrentPos()*25 &&
                      e.getX()<= DayGridPanel.this.columnLeftWidth + DayGridPanel.this.columnRightWidth &&  
                e.getX()<= testNote.getNoteZone().getX() + columnLeftWidth &&
                e.getY()>= testNote.getNoteZone().y &&e.getY()<= testNote.getNoteZone().y+ testNote.getNoteZone().height){
               setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
               break;
          }else{
            setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
          }
          }   
        }    */   
                        // Pour les notes                           
                        ListIterator iter = sortedList.listIterator(sortedList.size());               
                        while(iter.hasPrevious()){                 
                          Note testNote = (Note) iter.previous();
                          int i = testNote.getDate().get(Calendar.DAY_OF_YEAR)- workSpace.getCurrentDay().get(Calendar.DAY_OF_YEAR);
                          if (i ==0 ){
                          if (testNote.getNoteZone()!=null && testNote.getCalendrier().isDisplay())                                   
                            if (e.getX()>= DayGridPanel.this.columnLeftWidth + testNote.getConcurrentPos()*25 &&                       
                                e.getX()<= DayGridPanel.this.columnLeftWidth + DayGridPanel.this.columnRightWidth &&                    
                                e.getY()>= testNote.getNoteZone().y+6 &&e.getY()<= testNote.getNoteZone().y+ testNote.getNoteZone().height-6 ){     
                                setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
                                break;
                            }else if (e.getX()>= DayGridPanel.this.columnLeftWidth + testNote.getConcurrentPos()*25 &&                       
                                e.getX()<= DayGridPanel.this.columnLeftWidth + DayGridPanel.this.columnRightWidth &&                    
                                e.getY()>= testNote.getNoteZone().y-5 &&e.getY()<= testNote.getNoteZone().y+ 5 ){     
                                setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
                                break;
                            }else if (e.getX()>= DayGridPanel.this.columnLeftWidth + testNote.getConcurrentPos()*25 &&                       
                                e.getX()<= DayGridPanel.this.columnLeftWidth + DayGridPanel.this.columnRightWidth &&                    
                                e.getY()>= testNote.getNoteZone().y+ testNote.getNoteZone().height-5 &&
                                e.getY()<= testNote.getNoteZone().y+ testNote.getNoteZone().height+ 5 ){     
                                setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
                                break;
                            }
                              
                          }
                          setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                        }
                        
        repaint();        
      }

      
      public void mouseDragged(MouseEvent e) {}
    });
    
    this.addMouseListener(new MouseListener(){     
      public void mouseClicked(MouseEvent e) {        
        popup.setVisible(false);        
         popup.remove(es);
         popup.remove(ss);
         
      if (SwingUtilities.isRightMouseButton(e) && e.getX()>= columnLeftWidth){
                  if (workSpace.getSelectedNote()!=null){
                    if (!(workSpace.getSelectedNote().isDragEnable() || workSpace.getSelectedNote().isDragBottomEnable() || 
                        workSpace.getSelectedNote().isDragTopEnable() || dragBeginLunch || dragEndLunch  || dragBegin || dragEnd)){                     

                        // Pour les notes                           
                        ListIterator iter = sortedList.listIterator(sortedList.size());               
                        while(iter.hasPrevious()){                 
                          Note testNote = (Note) iter.previous();
                          int i = testNote.getDate().get(Calendar.DAY_OF_YEAR)- workSpace.getCurrentDay().get(Calendar.DAY_OF_YEAR);
                          if (i ==0 ){
                          if (testNote.getNoteZone()!=null && testNote.getCalendrier().isDisplay())                                   
                            if (e.getX()>= DayGridPanel.this.columnLeftWidth + testNote.getConcurrentPos()*25 &&                       
                                e.getX()<= DayGridPanel.this.columnLeftWidth + DayGridPanel.this.columnRightWidth &&                    
                                e.getY()>= testNote.getNoteZone().y &&e.getY()<= testNote.getNoteZone().y+ testNote.getNoteZone().height ){     

                                // On definit comme selectionnÃ©                      
                                testNote.setSelected(true);                         
                                workSpace.setSelectedNote(testNote);
                                popup.add(es);               
                                popup.add(ss);
                            }   
                          }
                        }                      
              
                        popup.setLocation((int)(DayGridPanel.this.getLocationOnScreen().getX() + e.getX()), (int)(DayGridPanel.this.getLocationOnScreen().getY() +e.getY()));
                        popup.setVisible(true); 
                    }
                  }else{
                    
                    // Pour les notes                           
                    ListIterator iter = sortedList.listIterator(sortedList.size());               
                    while(iter.hasPrevious()){                 
                      Note testNote = (Note) iter.previous();
                      int i = testNote.getDate().get(Calendar.DAY_OF_YEAR)- workSpace.getCurrentDay().get(Calendar.DAY_OF_YEAR);
                      if (i ==0 ){
                      if (testNote.getNoteZone()!=null && testNote.getCalendrier().isDisplay())                                   
                        if (e.getX()>= DayGridPanel.this.columnLeftWidth + testNote.getConcurrentPos()*25 &&                       
                            e.getX()<= DayGridPanel.this.columnLeftWidth + DayGridPanel.this.columnRightWidth &&                    
                            e.getY()>= testNote.getNoteZone().y &&e.getY()<= testNote.getNoteZone().y+ testNote.getNoteZone().height ){     

                            // On definit comme selectionnÃ©                      
                            testNote.setSelected(true);                         
                            workSpace.setSelectedNote(testNote);
                            popup.add(es);               
                            popup.add(ss);
                        }   
                      }
                    }                      
                    popup.setLocation((int)(DayGridPanel.this.getLocationOnScreen().getX() + e.getX()), (int)(DayGridPanel.this.getLocationOnScreen().getY() +e.getY()));
                    popup.setVisible(true);
                  }
                  return;
                }
      workSpace.setSelectedNote(null);
                                
        // Pour les zones de "temps libre"                    
        if (  e.getX()>= columnLeftWidth-10 && e.getX()<= columnLeftWidth &&                    
               e.getY()>= beginArrowY[2] && e.getY()<= beginArrowY[1] && !dragBegin){                          
          dragBeginLunch = false;
          dragEndLunch = false;
          dragBegin =true;
          dragEnd = false;       
        }else if (e.getX()>= columnLeftWidth-10 && e.getX()<= columnLeftWidth &&                   
                e.getY()>= endArrowY[2] && e.getY()<= endArrowY[1] && !dragEnd){                  
          dragBeginLunch = false;                  
          dragEndLunch = false;                  
          dragBegin =false;                  
          dragEnd = true;                  
        }else if (e.getX()>= columnLeftWidth-10 && e.getX()<= columnLeftWidth &&                      
                e.getY()>= beginLunchArrowY[2] && e.getY()<= beginLunchArrowY[1] && !dragBeginLunch){                  
          dragBeginLunch = true;                  
          dragEndLunch = false;                  
          dragBegin =false;                  
          dragEnd = false;                 
        }else if (e.getX()>= columnLeftWidth-10 && e.getX()<= columnLeftWidth &&                    
                e.getY()>= endLunchArrowY[2] && e.getY()<= endLunchArrowY[1] && !dragEndLunch){                 
          dragBeginLunch = false;                   
          dragEndLunch = true;      
          dragBegin =false;  
          dragEnd = false; 
        }else {                  
          dragBeginLunch = false;                 
          dragEndLunch = false;                
          dragBegin =false;                 
          dragEnd = false;                                
        }                              
        // Pour les notes                           
        ListIterator iter = sortedList.listIterator(sortedList.size());               
        while(iter.hasPrevious()){                 
          Note testNote = (Note) iter.previous();
          int i = testNote.getDate().get(Calendar.DAY_OF_YEAR)- workSpace.getCurrentDay().get(Calendar.DAY_OF_YEAR);
          if (i ==0 ){
          if (testNote.getNoteZone()!=null && testNote.getCalendrier().isDisplay())                                   
            if (e.getX()>= DayGridPanel.this.columnLeftWidth + testNote.getConcurrentPos()*25 &&                       
                e.getX()<= DayGridPanel.this.columnLeftWidth + DayGridPanel.this.columnRightWidth &&                    
                e.getY()>= testNote.getNoteZone().y-6 &&e.getY()<= testNote.getNoteZone().y+ testNote.getNoteZone().height+6 &&                     
                workSpace.getSelectedNote()==null){     
              // Double click, on ouvre la fenetre
              if (e.getClickCount()==2 && 
                      testNote.getDate().get(Calendar.DAY_OF_YEAR)== 
                      workSpace.getCurrentDay().get(Calendar.DAY_OF_YEAR)){       
                testNote.setDragBottomEnable(false);        
                testNote.setDragTopEnable(false);        
                testNote.setDragEnable(false);         
                notePanel.setVisible(true);  
                // Simple click         
              }   
              else if (e.getClickCount()==1 && !testNote.isDragEnable() && 
                      !testNote.isDragTopEnable()  && !testNote.isDragBottomEnable()){                            
                // Check le resize bord du haut                       
                if (e.getY()>= testNote.getNoteZone().y-9 &&e.getY()<= testNote.getNoteZone().y+ 6 )                                                     
                  testNote.setDragTopEnable(!testNote.isDragTopEnable());      
                  // Check le resize bord du bas                      
                else if (e.getY()>= testNote.getNoteZone().y+ testNote.getNoteZone().height-6 &&
                         e.getY()<= testNote.getNoteZone().y+ testNote.getNoteZone().height+ 9 )                          
                  testNote.setDragBottomEnable(!testNote.isDragBottomEnable());  
                  // Check le deplacement                      
                else if(testNote.isSelected())                            
                  testNote.setDragEnable(!testNote.isDragEnable());                                                                
              }else{                      
                testNote.setDragBottomEnable(false);                      
                testNote.setDragTopEnable(false);                     
                testNote.setDragEnable(false);                     
                testNote.setSelected(false);         
              } 
                // On definit comme selectionnÃ©                      
                testNote.setSelected(true);                         
                workSpace.setSelectedNote(testNote);
              // Si on a clickÃ© hors de la note                 
            }else{                    
            testNote.setDragBottomEnable(false);                    
            testNote.setDragTopEnable(false);                   
            testNote.setDragEnable(false);                   
            testNote.setSelected(false);                  
            }   
          }
        }
        // Double click, on ouvre la fenetre
        if (e.getClickCount()==2 && workSpace.getSelectedNote()==null){
                  Note note3 = new Note();
                  note3.setCalendrier(workSpace.getCurrentCalendrier());
                  workSpace.getCurrentCalendrier().addNote(note3);
                  int yPos = (int) (e.getY()-pas/2)/ pas ;

                  Calendar temp = Calendar.getInstance(workSpace.getCurrentDay().getTimeZone());
                  temp.add(Calendar.DAY_OF_YEAR, workSpace.getCurrentDay().get(Calendar.DAY_OF_YEAR)-temp.get(Calendar.DAY_OF_YEAR));      
                  temp.set(Calendar.HOUR_OF_DAY, (int)(yPos));
                  temp.set(Calendar.MINUTE, (((e.getY()-pas/2)% pas)<10? 0 :
                                              ((e.getY()-pas/2)% pas)<20? 15: 
                                              ((e.getY()-pas/2)% pas)<30? 30: 45 ));


                  Calendar temp2 = Calendar.getInstance();    
                  temp2.set(Calendar.HOUR_OF_DAY, (int)(yPos));  
                  temp2.add(Calendar.DAY_OF_YEAR, workSpace.getCurrentDay().get(Calendar.DAY_OF_YEAR)-temp.get(Calendar.DAY_OF_YEAR));              
                  temp2.add(Calendar.HOUR_OF_DAY, 1);

                  temp2.set(Calendar.MINUTE, (((e.getY()-pas/2)% pas)<10? 0 :
                                              ((e.getY()-pas/2)% pas)<20? 15: 
                                              ((e.getY()-pas/2)% pas)<30? 30: 45 ));    
                  note3.setDate(temp);
                  note3.setEndDate(temp2);

                  note3.setTitle("Nouvelle note");
                  note3.setSelected(true);
                  note3.setDragBottomEnable(true);
                  workSpace.setSelectedNote(note3);
                  noteList.add(note3);                              
        }
        
        repaint();
      }
      public void mousePressed(MouseEvent e) {}
      public void mouseReleased(MouseEvent e) {}
      public void mouseEntered(MouseEvent e) {}
      public void mouseExited(MouseEvent e) {}      
    });
  }

  public DayGridPanel(int line, int column, JPanel agendaNotePanel, Workspace workspace) {
    this (line, column, workspace);
    this.notePanel = agendaNotePanel;
  }

  
  void createGrid (Graphics g){
    g.setColor(Color.LIGHT_GRAY);
    
    // PointillÃ© demi heure
    for (int i = pas; i < pas*50; i+=pas){
      for (int j = columnLeftWidth ; j < getWidth()-4 ; j=j+3 )
        if ((j-columnRightWidth)%6 !=0)
          g.drawLine(j, i,j+3 , i);
    }  
    //Ligne heure pleine
    for (int i = 25, j = 0; i < pas*50; i+=pas, j++){
      g.drawLine(columnLeftWidth, i-5, getWidth()-4, i-5);
      g.drawString( (j<10? "0":"") + j + ":00", 15, i);      
    }
    
  }  
  
  void paintNote(Note testNote, Graphics2D g2d ){ 
    
    int PosX =columnLeftWidth+ testNote.getConcurrentPos()*25 ;
    //Zone de dessin
    if (testNote.getNoteZone()==null || testNote.getNoteZone().width!=columnRightWidth ){
      int PosY = testNote.getDate().get(Calendar.HOUR_OF_DAY)* pas + pas/2;
      // On est prÃ©cis Ã  la demi heure
      if (testNote.getDate().get(Calendar.MINUTE)>=45)
          PosY+=30;
      else if (testNote.getDate().get(Calendar.MINUTE)>=30)
        PosY+=20;
      else if (testNote.getDate().get(Calendar.MINUTE)>=15)
        PosY+=10;
 
      Calendar temp = Calendar.getInstance();
      temp.setTime( testNote.getEndDate().getTime());
      temp.add(Calendar.YEAR, -testNote.getDate().get(Calendar.YEAR));
      temp.add(Calendar.MONTH, -testNote.getDate().get(Calendar.MONTH));
      temp.add(Calendar.DAY_OF_MONTH, -testNote.getDate().get(Calendar.DAY_OF_MONTH));
      temp.add(Calendar.HOUR_OF_DAY, -testNote.getDate().get(Calendar.HOUR_OF_DAY));
      temp.add(Calendar.MINUTE, -testNote.getDate().get(Calendar.MINUTE));
      
      int longueur = temp.get(Calendar.HOUR_OF_DAY)*pas;   
      if (temp.get(Calendar.MINUTE)>=45)
          longueur+=30;
      else if (temp.get(Calendar.MINUTE)>=30)
        longueur+=20;
      else if (temp.get(Calendar.MINUTE)>=15)
        longueur+=10; 
      
      testNote.setNoteZone( new RoundRectangle2D.Float( PosX, PosY, columnRightWidth , longueur- 1, 20, 20));
      testNote.setYPos(pas/2  +PosY/ pas);
    }
   
    gradient = new GradientPaint((int)testNote.getNoteZone().getX(),(int)testNote.getNoteZone().getY(),testNote.getCalendrier().getColor(),
                                (int)testNote.getNoteZone().getX()+ (int)(testNote.getNoteZone().getWidth()*1.4),
                                (int)testNote.getNoteZone().getY(),new Color (255, 255,255));
   
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
    // On recupere la zone affichÃ© du composant
    clip1 = g2d.getClip();
    // On ne dessine que dans la note
    g2d.clip(testNote.getNoteZone());
    // et dans la zone affichÃ©
    g2d.setClip(clip1);
    // On veut une transparence de 40%
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));   
    // On defini la couleur du fond de la note
    g2d.setColor(testNote.getCalendrier().getColor());
    g2d.fillRoundRect( PosX, (int)(testNote.getNoteZone().y), 
            columnRightWidth- testNote.getConcurrentPos()*25 -5, (int)testNote.getNoteZone().height,18, 18);
    
    if (testNote.isSelected()){
      g2d.setColor(Color.WHITE);
      g2d.fillRoundRect( PosX, (int)(testNote.getNoteZone().y), 
              columnRightWidth- testNote.getConcurrentPos()*25 -5, (int)testNote.getNoteZone().height,18, 18);
    }
    // Bordure foncÃ©"
    g2d.setPaint(p1);   
    g2d.drawRoundRect( PosX, (int)(testNote.getNoteZone().y), 
            columnRightWidth- testNote.getConcurrentPos()*25-5, (int)testNote.getNoteZone().height- 1, 20, 20);        
    // Bordure de selection
    if (testNote.isSelected())
      g2d.setColor(Color.YELLOW);
    else 
      g2d.setPaint(p2);
    g2d.drawRoundRect( PosX+1, (int)(testNote.getNoteZone().y)+1, 
            columnRightWidth- testNote.getConcurrentPos()*25-6, (int)testNote.getNoteZone().height - 3, 18, 18); 
    

       // Bas de la note
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.9f));
    Rectangle rectClipBottom = new Rectangle (1+ PosX, (int)(testNote.getNoteZone().y+testNote.getNoteZone().getHeight())-20
            , columnRightWidth- testNote.getConcurrentPos()*15-5, 20);
    g2d.clip(rectClipBottom);
    g2d.setPaint(gradient);
    g2d.fillRoundRect( 1+ PosX, (int)(testNote.getNoteZone().y), 
            columnRightWidth- testNote.getConcurrentPos()*25-6, (int)testNote.getNoteZone().getHeight()-1,18, 18);
    if (testNote.isSelected()){
      g2d.setColor(Color.WHITE);
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
      g2d.fillRoundRect( 1+ PosX, (int)(testNote.getNoteZone().y), 
              columnRightWidth- testNote.getConcurrentPos()*25-6, (int)testNote.getNoteZone().getHeight()-1,18, 18);
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.9f));
    }   
    
    if (testNote.isSelected())
      g2d.setColor(Color.DARK_GRAY);
    else
      g2d.setColor(Color.WHITE);
    
    g2d.setFont(new java.awt.Font("Comic Sans MS", 1, 9));
    g2d.drawString( testNote.getDate().getTime().toString().substring(11, 16) +"->"+
                    testNote.getEndDate().getTime().toString().substring(11, 16), 
                     PosX +15, (int)(testNote.getNoteZone().y+testNote.getNoteZone().getHeight())-7);
    g2d.setClip(clip1);   
    
    // Titre de la note
    g2d.setPaint(gradient);
    Rectangle rectClip = new Rectangle (1+PosX, (int)(testNote.getNoteZone().y), 
            columnRightWidth- testNote.getConcurrentPos()*25-5, 20);
    g2d.clip(rectClip);
    g2d.fillRoundRect( 1+PosX, (int)(testNote.getNoteZone().y), 
            columnRightWidth- testNote.getConcurrentPos()*25-5, (int)testNote.getNoteZone().height,18, 18);
    
    if (testNote.isSelected()){
      g2d.setColor(Color.WHITE);
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
      g2d.fillRoundRect( 1+PosX, (int)(testNote.getNoteZone().y), 
              columnRightWidth- testNote.getConcurrentPos()*25-5, (int)testNote.getNoteZone().height,18, 18);
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.9f));
    }
    
    // On ecrit le text
    if (testNote.isSelected())
      g2d.setColor(Color.DARK_GRAY);
    else
      g2d.setColor(Color.WHITE);
    
    g2d.setFont(new java.awt.Font("Comic Sans MS", 1, 12));  
    g2d.drawString(testNote.getTitle(), 1+PosX +15, (int)(testNote.getNoteZone().y)+13);
    // On redefni le clip normal
    g2d.setClip(clip1);
    
    if (testNote.isSelected()){
         g2d.setColor(Color.DARK_GRAY);
         g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f));
        if (testNote.isDragBottomEnable()){         
          int arrowPosY = (int)(testNote.getNoteZone().getY()+ 
                  testNote.getNoteZone().getHeight())+1;
          int arrowPosX = columnLeftWidth+ testNote.getConcurrentPos()*25 + 
                  (columnRightWidth- testNote.getConcurrentPos()*25 )/2;
          resizeArrowX[0]=arrowPosX-7;
          resizeArrowX[1]=arrowPosX;
          resizeArrowX[2]=arrowPosX+7;
          resizeArrowY[0]=arrowPosY;
          resizeArrowY[1]=arrowPosY+7;
          resizeArrowY[2]=arrowPosY;
          g2d.fillPolygon(resizeArrowX, resizeArrowY, 3);
        }else if (testNote.isDragTopEnable()){
          int arrowPosY = (int)(testNote.getNoteZone().getY())+1;
          int arrowPosX = columnLeftWidth+ testNote.getConcurrentPos()*25 + 
                  (columnRightWidth- testNote.getConcurrentPos()*25 )/2;
          resizeArrowX[0]=arrowPosX-7;
          resizeArrowX[1]=arrowPosX;
          resizeArrowX[2]=arrowPosX+7;
          resizeArrowY[0]=arrowPosY;
          resizeArrowY[1]=arrowPosY-7;
          resizeArrowY[2]=arrowPosY;
          g2d.fillPolygon(resizeArrowX, resizeArrowY, 3);           
        }else if (testNote.isDragEnable()){
          int arrowPosY = (int)(testNote.getNoteZone().getY())+1;
          int arrowPosX = columnLeftWidth+ testNote.getConcurrentPos()*25 + 
                  (columnRightWidth- testNote.getConcurrentPos()*25 )/2;        
          resizeArrowX[0]=arrowPosX-7;
          resizeArrowX[1]=arrowPosX;
          resizeArrowX[2]=arrowPosX+7;
          resizeArrowY[0]=arrowPosY;
          resizeArrowY[1]=arrowPosY-7;
          resizeArrowY[2]=arrowPosY;
          g2d.fillPolygon(resizeArrowX, resizeArrowY, 3); 
          arrowPosY = (int)(testNote.getNoteZone().getY()+ 
                  testNote.getNoteZone().getHeight())-1;
          resizeArrowY[0]=arrowPosY;
          resizeArrowY[1]=arrowPosY+7;
          resizeArrowY[2]=arrowPosY;
          g2d.fillPolygon(resizeArrowX, resizeArrowY, 3);         
        }
    }
  }
    
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    // En cas de redimensionnement
    columnRightWidth = getWidth() -100;   
    // Dessin de la grille
    //createGrid(g);
    // Positionnement en X des fleches
    ArrowX[0]= columnLeftWidth;    
    ArrowX[1]= columnLeftWidth-10; 
    ArrowX[2]= columnLeftWidth-10;
    // Recuperation du graphics de dessin
    g2d = (Graphics2D)g.create();
    // On va dessiner en transparence
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));    
    //Zone pour le matin
    r2d1 = new RoundRectangle2D.Float(columnLeftWidth , 0, 
            columnRightWidth- 5, beginArrowY[0], 20, 20);
    // Pour le soir
    r2d2 = new RoundRectangle2D.Float(columnLeftWidth,endArrowY[0] ,
            columnRightWidth- 5,getHeight() -20 - endArrowY[0], 20, 20);
    // Pour le midi
    r2d3 = new RoundRectangle2D.Float(columnLeftWidth, beginLunchArrowY[0],
            columnRightWidth- 5, endLunchArrowY[0]-beginLunchArrowY[0], 20, 20);
    // Zone affichÃ©
    clip1 = g2d.getClip(); 
    
    //Aujourd'hui ??
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
    
    
    Calendar temp1 = Calendar.getInstance();
    temp1.setTime(workSpace.getCurrentDay().getTime());
    
    int i = temp1.get(Calendar.DAY_OF_YEAR)- Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
    if (i ==0 ){
          System.out.println(" AUJOURD'HUI");
          r2d4 = new RoundRectangle2D.Float(columnLeftWidth, 0, columnRightWidth, getHeight()-20, 20, 20);
             // Zone du matin
          g2d.clip(r2d4);
          g2d.setColor(new Color(200,200, 255));
          g2d.fillRoundRect(columnLeftWidth, 0, columnRightWidth-2, getHeight()-20, 20, 20);
          g2d.setClip(clip1);
          //g2d.setPaint(p1);
          g2d.setColor(new Color(130,130, 255));
          g2d.drawRoundRect(columnLeftWidth, 0, columnRightWidth-1, getHeight()-20, 20, 20);
          //g2d.setPaint(p2);
          g2d.setColor(new Color(200,200, 255));
          g2d.drawRoundRect(columnLeftWidth, 0, columnRightWidth-2, getHeight()-21, 18, 18);
    }
    
    createGrid(g);
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f));
    
    // Zone du matin
    g2d.clip(r2d1);
    g2d.setPaint(p2);
    g2d.fillRect(columnLeftWidth, 0, columnRightWidth-4, beginArrowY[0]);
    g2d.setClip(clip1);
    g2d.setPaint(p1);
    g2d.drawRoundRect(columnLeftWidth, 0, columnRightWidth- 5, beginArrowY[0], 20, 20);
    g2d.setPaint(p2);
    g2d.drawRoundRect(columnLeftWidth+1, 1, columnRightWidth - 7, beginArrowY[0] - 3, 18, 18);   
    if (dragBegin)
      g2d.setColor(Color.RED);
    else
      g2d.setColor(Color.BLACK);
    g2d.fillPolygon(ArrowX, beginArrowY, 3);   
    //Zone du soir
    g2d.clip(r2d2);
    g2d.setPaint(p2);
    g2d.fillRect(columnLeftWidth, endArrowY[0], 
            columnRightWidth - 5, getHeight() -20 - endArrowY[0] );
    g2d.setClip(clip1);
    g2d.setPaint(p1);
    g2d.drawRoundRect(columnLeftWidth, endArrowY[0], 
            columnRightWidth- 5, getHeight() -20- endArrowY[0], 20, 20);
    g2d.setPaint(p2);
    g2d.drawRoundRect(columnLeftWidth+1, endArrowY[0] + 1, 
            columnRightWidth - 7, getHeight() -20- endArrowY[0]- 3, 18, 18); 
    if (dragEnd)
      g2d.setColor(Color.RED);
    else
      g2d.setColor(Color.BLACK);
    g2d.fillPolygon(ArrowX, endArrowY, 3);       
    //Zone du Midi
    g2d.clip(r2d3);
    g2d.setPaint(p2);
    g2d.fillRect(columnLeftWidth, beginLunchArrowY[0], 
            columnRightWidth -5, endLunchArrowY[0]-beginLunchArrowY[0]);
    g2d.setClip(clip1);
    g2d.setPaint(p1);
    g2d.drawRoundRect(columnLeftWidth, beginLunchArrowY[0], 
            columnRightWidth- 5, endLunchArrowY[0]-beginLunchArrowY[0], 20, 20);
    g2d.setPaint(p2);
    g2d.drawRoundRect(columnLeftWidth+1, beginLunchArrowY[0]+ 1, 
            columnRightWidth - 7,endLunchArrowY[0]-beginLunchArrowY[0]- 3, 18, 18); 
    if (dragBeginLunch)
      g2d.setColor(Color.RED);
    else
      g2d.setColor(Color.BLACK);
    g2d.fillPolygon(ArrowX, beginLunchArrowY, 3);
    if (dragEndLunch)
      g2d.setColor(Color.RED);
    else
      g2d.setColor(Color.BLACK);
    g2d.fillPolygon(ArrowX, endLunchArrowY, 3);


    
    
    
    updateConcurrent();
    // On dessine les notes
    for (Note testNote : sortedList){
      if (testNote.getDate().get(Calendar.DAY_OF_YEAR) == 
              workSpace.getCurrentDay().get(Calendar.DAY_OF_YEAR)&& testNote.getCalendrier().isDisplay()){
        paintNote(testNote,g2d);
      }
    }
    
    if (workSpace.getSelectedNote()!=null && 
            workSpace.getSelectedNote().getDate().get(Calendar.DAY_OF_YEAR)== 
            workSpace.getCurrentDay().get(Calendar.DAY_OF_YEAR)){
      
      if (workSpace.getSelectedNote().getCalendrier().isDisplay())
       paintNote(workSpace.getSelectedNote(),g2d);
      else
        workSpace.setSelectedNote(null);
    
    }
    g2d.dispose();
  }

    int getConcurentCout(Note note){
    ListIterator iter = sortedList.listIterator(sortedList.indexOf(note));
      while(iter.hasPrevious()){
        Note temp = (Note) iter.previous();
        if (note.getNoteZone()!=null && temp.getNoteZone()!=null && 
                note.getCalendrier().isDisplay() && temp.getCalendrier().isDisplay())
          if (note.getDate().get(Calendar.DAY_OF_YEAR)== temp.getDate().get(Calendar.DAY_OF_YEAR))
            if ( (note.getNoteZone().getY()>=temp.getNoteZone().getY() && 
                 note.getNoteZone().getY()< temp.getNoteZone().getY()+ temp.getNoteZone().getHeight()) ||
                 (temp.getNoteZone().getY()>=note.getNoteZone().getY() && 
                 temp.getNoteZone().getY()< note.getNoteZone().getY()+ note.getNoteZone().getHeight() ) &&            
                 (note != temp) ){
                  return temp.getConcurrentPos()+1;              
            }
      }    
    return 0;
  }
  void updateConcurrent(){
    if(noteList.size()>0 ){
      sortedList = new LinkedList<Note>();
      sortedList.add(noteList.element());
      noteList.element().setConcurrent(0);
      noteList.element().setConcurrentPos(0);

      for ( Note testNote1 : noteList.subList(1, noteList.size()) ){
          // Initialisation des valeurs
          testNote1.setConcurrent(0);
          testNote1.setConcurrentPos(0);   
          // CrÃ©ation de la liste triÃ©e
          ListIterator iter = sortedList.listIterator();
          // Tant qu'il y a un element dans la list ou que celui ci est infÃ©rieur Ã  la valeur
          // et si les element graphiques sont crÃ©Ã©es
          while(iter.hasNext() && testNote1.getNoteZone()!=null
                  && sortedList.get(iter.nextIndex()).getNoteZone()!= null &&
                  sortedList.get(iter.nextIndex()).getNoteZone().getHeight()> 
                  testNote1.getNoteZone().getHeight() ){
            // On avance
            iter.next();
          }
          if (testNote1.getCalendrier().isDisplay())
          iter.add(testNote1);      
      }

      // On trouve le nombre de pere de chaque note
      // (nombre maximum de note concurrente
      for ( Note testNote1 : sortedList ){        
        testNote1.setConcurrentPos(getConcurentCout(testNote1));
      }
    }
  }
  
}
